import L from "leaflet";
/** 解决瓦片接边处白线问题 */
L.TileLayer.Canvas = L.TileLayer.extend({
  _delays: {},
  _delaysForZoom: null,
  createCanvas: function (e, t, a) {
    var s,
      o = e.getContext("2d"),
      i = this.options.doubleSize,
      n = this.getTileSize(),
      r = n.x,
      l = n.y;
    e.width = i ? 2 * r : r;
    e.height = i ? 2 * l : l;
    var c = new Image();
    c.onload = function () {
      try {
        o.drawImage(c, 0, 0);
        e.complete = !0;
      } catch (e) {
        s = e;
      } finally {
        a(s, e);
      }
    };
    var h = this._getZoomForUrl();
    c.src = isNaN(h) ? "" : this.getTileUrl(t);
    c.crossOrigin = "anonymous";
  },
  createTile: function (e, t) {
    var a = this,
      s = this.options.timeout,
      o = e.z,
      i = document.createElement("canvas");
    return (
      s
        ? (o !== this._delaysForZoom &&
            (this._clearDelaysForZoom(), (this._delaysForZoom = o)),
          this._delays[o] || (this._delays[o] = []),
          this._delays[o].push(
            setTimeout(function () {
              a.createCanvas(i, e, t);
            }, s)
          ))
        : this.createCanvas(i, e, t),
      i
    );
  },
  _clearDelaysForZoom: function () {
    var e = this._delaysForZoom;
    var t = this._delays[e];
    t.forEach(function (e, a) {
      clearTimeout(e);
      delete t[a];
    });
    delete this._delays[e];
  },
});

L.tileLayer.canvas = function (e, t) {
  return new L.TileLayer.Canvas(e, t);
};

export default L.tileLayer.canvas;
